/*
测试客户端的实现：
细节：
    1.服务端需要从用户子服务获取用户信息 --- 得启动用户子服务，并注册一个用户
    2.获取转发列表 -- 在数据库中造假数据（会话成员信息）
    3.接下来进行测试
*/

//speech_server的测试客户端实现
//1.进行服务发现 -- 发现speech_server的服务器节点地址信息并实例化的信道通信
//2.读取语音文件数据
//3.发起语音识别RPC调用

#include "etcd.hpp"
#include "channel.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "base.pb.h"
#include "transmite.pb.h"
#include "utils.hpp"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "服务监控根目录");

std::shared_ptr<zrb::ServiceManager> sm;

void string_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    zrb::transmite_service_Stub stub(channel.get());
    zrb::NewMessageReq req;
    zrb::GetTransmitTargetRsp rsp;
    req.set_request_id(zrb::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zrb::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}
void image_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    zrb::transmite_service_Stub stub(channel.get());
    zrb::NewMessageReq req;
    zrb::GetTransmitTargetRsp rsp;
    req.set_request_id(zrb::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zrb::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void speech_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    zrb::transmite_service_Stub stub(channel.get());
    zrb::NewMessageReq req;
    zrb::GetTransmitTargetRsp rsp;
    req.set_request_id(zrb::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zrb::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void file_message(const std::string &uid, const std::string &sid, 
    const std::string &filename, const std::string &content) {
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    zrb::transmite_service_Stub stub(channel.get());
    zrb::NewMessageReq req;
    zrb::GetTransmitTargetRsp rsp;
    req.set_request_id(zrb::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zrb::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_name(filename);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    zrb::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1. 先构造Rpc信道管理对象
    sm = std::make_shared<zrb::ServiceManager>();
    sm->declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&zrb::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&zrb::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    //2. 构造服务发现对象
    zrb::Discovery::ptr dclient = std::make_shared<zrb::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    // //3. 通过Rpc信道管理对象，获取提供Echo服务的信道
    // auto channel = sm->choose(FLAGS_transmite_service);
    // if (!channel) {
    //     std::this_thread::sleep_for(std::chrono::seconds(1));
    //     return -1;
    // }
    // //4. 发起EchoRpc调用（通过rpc信道）
    // zrb::transmite_service_Stub stub(channel.get());
    // zrb::NewMessageReq req;
    
    // std::cout << "正常运行222" << std::endl;

    // //5.设置请求
    // req.set_request_id("消息转发子模块测试");
    // req.set_user_id("0996-2792d4c5-0000");
    // req.set_chat_session_id("会话ID1");
    // req.mutable_message()->set_message_type(zrb::MessageType::STRING);
    // req.mutable_message()->mutable_string_message()->set_content("测试消息内容");

    // std::cout << "正常运行333" << std::endl;

    // brpc::Controller cntl;
    // zrb::GetTransmitTargetRsp rsp;
    // stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    // if (cntl.Failed() == true) {
    //     std::cout << "Rpc调用失败：" << cntl.ErrorText() << std::endl;
    //     std::this_thread::sleep_for(std::chrono::seconds(1));
    //     return -1;
    // }

    // std::cout << "正常运行444" << std::endl;

    // std::cout << "请求id " << rsp.request_id() << std::endl;
    // std::cout << "消息id " << rsp.message().message_id() << std::endl;
    // std::cout << "消息所属聊天会话ID " << rsp.message().chat_session_id() << std::endl;
    // std::cout << "消息产生的时间 " << rsp.message().timestamp() << std::endl;
    // std::cout << "消息发送者的信息 " << rsp.message().sender().nickname() << std::endl;
    // std::cout << "信息 " << rsp.message().message().string_message().content() << std::endl;
    // for(auto user_id : rsp.target_id_list())
    // {
    //      std::cout << "target : " << user_id << std::endl;
    // }

    // string_message("0996-2792d4c5-0000", "会话ID1", "吃饭了吗？");
    // string_message("03b6-4c692527-0000", "会话ID1", "吃的盖浇饭！！");
    // image_message("03b6-4c692527-0000", "会话ID1", "可爱表情图片数据");
    // speech_message("0996-2792d4c5-0000", "会话ID1", "动听猪叫声数据");

    file_message("03b6-4c692527-0000","f7f7-e2bad92a-0000","猪爸爸的文件","猪爸爸的文件数据");

    return 0;
}

